package com.education.websocket.config;

import com.alibaba.fastjson.JSON;

import javax.websocket.EncodeException;
import javax.websocket.Encoder;
import javax.websocket.EndpointConfig;

/**
 * @Author: liftsail
 * @Date: 2022/10/14 14:33
 * @Description: 不积跬步无以至千里
 * <p>
 * 配置WebSocket编码器，用于发送请求的时候可以发送Object对象，实则是json数据   sendObject()
 */
public class ServerEncoder implements Encoder.Text<Object> {

    @Override
    public void destroy() {
        // TODO Auto-generated method stub

    }

    @Override
    public void init(EndpointConfig arg0) {
        // TODO Auto-generated method stub

    }

    //@Override
    //public String encode(List list) throws EncodeException {
    //    try {
    //        return JSON.toJSON(list).toString();
    //    } catch (Exception e) {
    //        // TODO Auto-generated catch block
    //        e.printStackTrace();
    //        return "";
    //    }
    //}

    @Override
    public String encode(Object object) throws EncodeException {
        try {
            return JSON.toJSON(object).toString();
        } catch (Exception e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
            return "";
        }
    }

}

